/**
 * @(#) NoticeViewMediator.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	공지사항 - Mediator
 * 				공지사항 Presentation Logic을 관리
 */
package com.skt.common_hr.allapp.view
{
	import com.skt.common_hr.allapp.AllappFacade;
	import com.skt.common_hr.allapp.events.NoticeEvent;
	import com.skt.common_hr.allapp.model.NoticeViewProxy;
	import com.skt.common_hr.allapp.model.vo.NoticeDetailVO;
	import com.skt.common_hr.allapp.view.components.AllappList;
	import com.skt.common_hr.allapp.view.components.NoticeView;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonFacade;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.http.HTTPService;
	
	import org.puremvc.as3.multicore.interfaces.INotification;


	public class NoticeOverViewMediator extends OverViewMediator
	{
		public static const NAME:String = "NoticeViewMediator";

		private var currentArticle:NoticeDetailVO;
		private var contentIdx:String;
		private var deleteFile:HTTPService;


		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function NoticeOverViewMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			super.initializeView();
 		}

 		override protected function activatedView():void
 		{
 			super.activatedView();
			view.vsNotice.selectedIndex = 0;
			loadNoticeList();
			setEventListeners();
 		}

 		override protected function closedView():void
 		{
 			super.closedView();
 			removeEventListeners();
 		}

		private function setEventListeners():void
		{
			view.addEventListener(AllappList.NoticeAllData, loadNoticeList);
			view.addEventListener(AllappList.NoticeKeySelect, onNoticeSearch);
			view.addEventListener(AllappList.NoticeWrite, onNoticeWrite);
			view.addEventListener(AllappList.NoticeReload, onNoticeReloadList);
			view.addEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);

			view.noticeDetail.addEventListener(NoticeView.NoticeShCancel, onNoticeDetailCancel);
			view.noticeDetail.addEventListener(NoticeEvent.MODIFY_ITEM, onNoticeModify);
		}

 		private function removeEventListeners():void
 		{
			view.removeEventListener(AllappList.NoticeAllData, loadNoticeList);
			view.removeEventListener(AllappList.NoticeKeySelect, onNoticeSearch);
			view.removeEventListener(AllappList.NoticeWrite, onNoticeWrite);
			view.removeEventListener(AllappList.NoticeReload, onNoticeReloadList);
			view.removeEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);

			view.noticeDetail.removeEventListener(NoticeView.NoticeShCancel, onNoticeDetailCancel);
			view.noticeDetail.removeEventListener(NoticeEvent.MODIFY_ITEM, onNoticeModify);
 		}

 		//공지사항 쓰기/수정 버튼 권한체크
 		private function setAuthority1():void
 		{
 			view.writeBtn.visible = false;
			for each(var auth:* in Variables.authorities)
			{
				//trace(auth.EMENU + ", " +auth.EMERL );
				if(auth.EMENU == "ALLAPP") {
					if(auth.EMERL == "W") {
						view.writeBtn.visible = true;
						view.noticeDetail.editBtnValue = true;
						//view.chkPastView.visible = true;
					}
				}
			}
 		}

  		private function loadNoticeList(event:Event = null):void
 		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.loadList();
			//버튼권한체크
			setAuthority1();
 		}


 		private function onNoticeReloadList(event:Event = null):void
 		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.loadList();
 		}

		private function increaseViewCount(index:Number):void
		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.increaseCount(index);
		}







		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					AllappFacade.LOAD_NOTICE_COMPLETE,
					AllappFacade.MODE_LIST_NOTICE
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				case AllappFacade.LOAD_NOTICE_COMPLETE:
					displayNoticeList(note.getBody() as NoticeViewProxy);
					break;
				case AllappFacade.MODE_LIST_NOTICE:
					view.vsNotice.selectedIndex = 0;
					if(Boolean(note.getBody())) {
						loadNoticeList();
					}
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}



		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
		 
		public var acT_SCODE:ArrayCollection;	// 검색옵션코드
		public var acT_GCODE:ArrayCollection;	// 구분코드
		
		public var sE_APPORGEH:String;	// 대표코드
		public var sE_APPORGTX:String;	// 발령권자
		
		public var sE_APPORGEH1:String;	// 대표코드
		public var sE_APPORGTX1:String;	// 발령부서		
		
 		private function displayNoticeList(proxy:NoticeViewProxy):void
 		{
 			view.pageNavigator.TotalPageCount = proxy.rowCount;	//총페이지
 			view.dataProvider = proxy.noticeList;

			// 공통 코드 셋팅..
 			acT_SCODE = proxy.acT_SCODE;
 			acT_GCODE = proxy.acT_GCODE; 			
 			sE_APPORGEH = proxy.sE_APPORGEH;
 			sE_APPORGTX = proxy.sE_APPORGTX;
 			
 			sE_APPORGEH1 = proxy.sE_APPORGEH1;
 			sE_APPORGTX1 = proxy.sE_APPORGTX1; 			
 			
 			var obj:Object = {};
 			obj.acT_GCODE 	= acT_GCODE;
 			obj.sE_APPORGEH = sE_APPORGEH;
 			obj.sE_APPORGTX = sE_APPORGTX;
 			obj.sE_APPORGEH1 = sE_APPORGEH1;
 			obj.sE_APPORGTX1 = sE_APPORGTX1; 			
 			
 			
 			if(ArrayCollection(view.cbSearchgb.dataProvider).length > 0) {
 					
 			}else {
 				view.cbSearchgb.dataProvider = acT_SCODE; // 검색코드.
 			}
 			
 			AllappList(view).ojbCommon = obj;
 			
 			CommonFacade(facade).setStatusMessage("STATUS_COMPLETE_LOAD_NOTICE_LIST", [view.dataProvider.length]);
 		}



		private function onNoticeSearch(event:Event):void
 		{
 			if(view.keyWordNm == "") {
 				Alert.show("검색어를 입력하세요");
 				return ;
 			}
 			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.keywordSearch(view.cbSearchgb.selectedItem.CODE, view.keyWordNm);
 		}





 
















		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onNoticeWrite(event:Event):void
 		{
			view.vsNotice.selectedIndex = 2;
 			facade.sendNotification(AllappFacade.MODE_WRITE_NOTICE, this);
 		}


		private function onDetailView(event:NoticeEvent):void
		{
			view.vsNotice.selectedIndex = 1;
			view.noticeDetail.dataProvider = event.selectedItem;
			increaseViewCount(event.selectedItem.CONTENT_IDX);
		}

		private function onNoticeDetailCancel(event:Event):void
		{
			view.vsNotice.selectedIndex = 0;
			loadNoticeList();
		}

 		private function onNoticeModify(event:Event):void
 		{
			view.vsNotice.selectedIndex = 2;
			facade.sendNotification(AllappFacade.MODE_MODIFY_NOTICE, view.noticeDetail.dataProvider);
 		}



 		private function clearWriteView():void
 		{
 			currentArticle = null;
			view.noticeWrite.dataProvider = null;
			view.noticeWrite.content.htmlText = "";
 		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():AllappList
        {
            return viewComponent as AllappList;
        }
	}
}